package com.nmh.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nmh.dto.PastureDto;
import com.nmh.entity.Pasture;
import com.nmh.mapper.PastureMapper;
import com.nmh.service.PastureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PastureServiceImpl extends ServiceImpl<PastureMapper, Pasture> implements PastureService {
    @Autowired
    private PastureMapper pastureMapper;
    @Autowired
    private PastureService pastureService;
    @Override
    public List<Pasture> selectAll() {
        return pastureMapper.selectAll();
    }

    @Override
    public List<Pasture> SelectAllByState() {
        return pastureMapper.selectAllByState();
    }
    @Override
    public boolean insertPasture(PastureDto pastureDto){
        Pasture pasture=new Pasture();
        pasture.setPastureName(pastureDto.getPastureName());
        pasture.setAddress(pastureDto.getAddress());
        pasture.setLatitude(pastureDto.getLatitude());
        pasture.setLongitude(pastureDto.getLongitude());
//        pasture.setLivestockId(pastureDto.getLivestockId());
        pasture.setSiteSize(pastureDto.getSiteSize());
        pasture.setCultivationSize(pastureDto.getCultivationSize().toString());
        boolean res=pastureService.save(pasture);
        if(res){
            return true;
        }
        return false;
    }
    @Override
    public boolean searchPasture(String pastureName){
        long res=pastureMapper.selectCount(new QueryWrapper<Pasture>().eq("pasture_name",pastureName));
        if(res==0){
            return true;
        }
        return false;
    }
    @Override
    public boolean updatePasture(PastureDto pastureDto) {
        Pasture pasture=new Pasture();
        pasture.setPastureId(pasture.getPastureId());
        pasture.setLatitude(pastureDto.getLatitude());
        pasture.setPastureName(pastureDto.getPastureName());
        pasture.setAddress(pastureDto.getAddress());
        pasture.setLongitude(pastureDto.getLongitude());
        pasture.setLivestockId(pastureDto.getLivestockId());
        pasture.setSiteSize(pastureDto.getSiteSize());
        pasture.setCultivationSize(pastureDto.getCultivationSize().toString());
        int res=pastureMapper.update(pasture,new QueryWrapper<Pasture>().eq("pasture_id",pastureDto.getPastureId()));
        return res>0;
    }
    @Override
    public Pasture searchPastureByName(String pastureName) {
        Pasture pasture=pastureMapper.selectOne(new QueryWrapper<Pasture>().eq("pasture_name",pastureName));
        return pasture;
    }
    @Override
    public String searchPastureById(Integer id) {
        String name=pastureMapper.selectById(id).getPastureName();
        return name;
    }
}
